/** \file loc_msgmanager.c This file contains definitions of variables and functions of the message manager module.
*/
#include "loc_msgmanager.h"


/****************/
/*	Local Types */
/****************/




typedef struct 	{
					u8 	u8Text[MSG_MESSAGE_STORAGE_LENGTH];
					u8	u8TextLength;
				} MSG_tstrMessage;


typedef	struct	{
				MSG_tstrMessage strMessage[MSG_NUM_OF_STORED_MESSAGES];
				u8				u8CurrentMessage;
				} MSG_tstrMessageList;





/*********************/
/*	Local Dfinitions */
/*********************/

#define 	u8FIRST_MSG					(u8)0
#define 	u8SECOND_MSG				(u8)1
#define 	u8THIRD_MSG					(u8)2
#define 	u8FIRST_AND_SECOND_MSG		(u8)3
#define 	u8ALL_MSG					(u8)4


#define	au8FIXED_MESSAGE					{26,27,28,29}

#define u8FIXED_MESSAGE_LENGTH				(u8)4

#define u16MAX_MESSAGE_LENGTH				(u16)507

#define u8NUMBER_OF_COL_IN_CHAR             (u8)5

/*******************/
/*	Local Variables*/
/*******************/

static const u8 au8FixedMessage[] = au8FIXED_MESSAGE;

const u8 PROGMEM MSG_u8LUT[] = {//	Col1	Col2	Col3	Col4	Col5
									0,      0,      0,      0,      0,      //Char  0(Space)
                                    124,    18,     17,     18,     124,    //Char	1(A)
									127,    73,     73,     73,     54,     //Char	2(B)
									62,     65,     65,     65,     34,  	//Char	3(C)
									127,    65,     65,     34,     28,   	//Char	4(D)
									127,    73,     73,     73,     65,    	//Char	5(E)
									127,    9,      9,      9,      1,      //Char	6(F)
                                    62,     65,     73,     73,     58,     //Char	7(G)
                                    127,    8,      8,      8,      127,    //Char	8(H)
                                    0,      65,     127,    65,     0,      //Char	9(I)
                                    48,     64,     65,     63,     1,      //Char	10(J)
                                    127,    8,      20,     34,     65,     //Char	11(K)
                                    127,    64,     64,     64,     64,     //Char	12(L)
                                    127,    2,      4,      2,      127,    //Char	13(M)
                                    127,    3,      12,     48,     127,    //Char	14(N)
                                    62,     65,     65,     65,     62,     //Char	15(O)
                                    127,    9,      9,      6,      0,      //Char	16(P)
                                    12,     18,     33,     50,     76,     //Char	17(Q)
                                    127,    13,     21,     34,     64,     //Char	18(R)
                                    38,     73,     73,     81,     34,     //Char	19(S)
                                    1,      1,      127,    1,      1,      //Char	20(T)
                                    63,     64,     64,     64,     63,     //Char	21(U)
                                    7,      56,     64,     56,     7,      //Char	22(V)
                                    63,     64,     56,     64,     63,     //Char	23(W)
                                    99,     20,     8,      20,     99,     //Char	24(x)
                                    3,      4,      120,    4,      3,      //Char	25(Y)
                                    65,     97,     89,     69,     67,     //Char	26(Z)
                                    0,      2,      65,     127,    64,     //Char	27(1)
                                    98,     49,     41,     37,     66,     //Char	28(2)
                                    33,     65,     73,     77,     51,     //Char	29(3)
                                    28,     18,     17,     127,    16,     //Char	30(4)
                                    47,     73,     73,     73,     49,     //Char	31(5)
                                    62,     73,     73,     73,     49,     //Char	32(6)
                                    3,      65,     49,     13,     3,      //Char	33(7)
                                    54,     73,     73,     73,     54,     //Char	34(8)
                                    38,     73,     73,     73,     62,     //Char	35(9)
                                    62,     97,     93,     67,     62,      //Char	36(0)
									14, 	17, 	34, 	17, 	14, 	//Char  37(Heart)

								};

static MSG_tstrMessageList 	EEMEM   strMessageList = 	{
															{	//strMessage
																{	//Message1 
																	{12,5,4,4,9,19,0}, 	//Text
																	7				//Length
																},

																{	//Message2
																	{6,7,8,9,0,0},		//Text	
																	6				//Length
																},

																{ 	//Message3
																	{10,11,12,13,14,25,32,35,1,29,4,0,0},	//Text	
																	13				//Length
																},
															},
															
																//u8CurrentMessage
																0
															
														};

static TYP_tu8Bool                  bEnablePeriodicSave;

//static u8 				            au8MessageBufferText[u16MAX_MESSAGE_LENGTH];
			
static u8				            u8CurrentMessage;

static u8				            u8StoredMsgIndexer;

static u16				            u16MsgIndexer;

static u16				            u16MessageLength;


/*********************************/
/*	Local Functions' Declaration */
/*********************************/


/***********************************/
/*	External Functions' Definition */
/***********************************/

MSG_tu16GetChar		        MSG_u16GetReadyChar;


void MSG_vidInit(void)
{
	
	//u8 u8Indexer;

	u8CurrentMessage = eeprom_read_byte(&(strMessageList.u8CurrentMessage));
	
	
	u16MessageLength = eeprom_read_byte(

					&(strMessageList.strMessage[u8CurrentMessage].u8TextLength));

//	for (u8Indexer = 0; u8Indexer < u16MessageLength; u8Indexer++ )
//	{
//		au8MessageBufferText[u8Indexer] =
//					eeprom_read_byte(
//
//					&(strMessageList.strMessage[u8CurrentMessage].u8Text[u8Indexer]));
//	
//	}
//	
//	
  MSG_u16GetReadyChar = (MSG_tu16GetChar)MSG_u16GetRamChar;
    

}


void MSG_vidStartSavingMessages(void)
{

    MSG_u16GetReadyChar = MSG_u16GetStoredChar;

    u16MessageLength = TYP_u16ZERO;

}


TYP_tu8Bool MSG_bAddToMsgBuff(const u8 * const pu8Buffer, u8 u8Length )
{

    u16 u16TotalLength;
    TYP_tu8Bool bReturnValue;

    u16TotalLength = u16MessageLength + (u16)u8Length;

    if (u16TotalLength <= u16MAX_MESSAGE_LENGTH )
    {
        
        //utl_vidMemCopyByte( pu8Buffer, &au8MessageBufferText[u16MessageLength],u8Length); 
        u16MessageLength = u16TotalLength;
        bReturnValue = TYP_u8TRUE;
    }
    else 
    {
        bReturnValue = TYP_u8FALSE;
    }

    return bReturnValue;
}




void MSG_vidSaveMessage(u8 u8MessageNumber)
{
    bEnablePeriodicSave = TYP_u8TRUE;

    u16MsgIndexer = TYP_u16ZERO;

    u8CurrentMessage = u8MessageNumber;
}


void MSG_vidMainFunction(void)
{



    if (bEnablePeriodicSave == TYP_u8TRUE)
    {

        if (u16MsgIndexer >= u16MessageLength)
        {
            u16MsgIndexer = TYP_u16ZERO;

            eeprom_write_byte(&(strMessageList.strMessage[u8CurrentMessage].u8TextLength),
                        u16MessageLength);

            u16MessageLength = TYP_u16ZERO;

            bEnablePeriodicSave = TYP_u8FALSE;

//            MSG_vidSAVE_FUNCTION_END_CBK();
        }
        else
        {
           // eeprom_write_byte(&(strMessageList.strMessage[u8CurrentMessage].u8Text[u16MsgIndexer]),
           //                au8MessageBufferText[u16MsgIndexer] );
    
            u16MsgIndexer++;
        }

        

    }

}

void MSG_vidUpdateMessage(u8 MsgNumber)
{
        eeprom_write_byte(&(strMessageList.u8CurrentMessage),MsgNumber);

        MSG_u8RESET_MCU;
}

u16 MSG_u16GetStoredChar(void)
{
    
    u8 u8TempIndex;

    u8TempIndex = au8FixedMessage[u8StoredMsgIndexer] * u8NUMBER_OF_COL_IN_CHAR ;

    u8StoredMsgIndexer++;
    
    if ( u8StoredMsgIndexer >= u8FIXED_MESSAGE_LENGTH )
    {
        u8StoredMsgIndexer = TYP_u8ZERO;
    }
    return (u16)u8TempIndex;
    

}

u16 MSG_u16GetRamChar(void)
{
    u16 u16TempIndex;

    //u16TempIndex = au8MessageBufferText[u16MsgIndexer] * u8NUMBER_OF_COL_IN_CHAR ;

	u16TempIndex = eeprom_read_byte(
	
					&(strMessageList.strMessage[u8CurrentMessage].u8Text[u16MsgIndexer]));
	u16TempIndex = u16TempIndex * (u16)u8NUMBER_OF_COL_IN_CHAR;

    u16MsgIndexer++;
    
    if ( u16MsgIndexer >= u16MessageLength )
    {
        u16MsgIndexer = TYP_u16ZERO;
    }
    return u16TempIndex;

    
}
/********************************/
/*	Local Functions' Definition */
/********************************/
